package com.flyduck.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyduck.common.exception.BusinessException;
import com.flyduck.common.util.CollectionUtil;
import com.flyduck.common.util.StringUtil;
import com.flyduck.common.util.TokenUtil;
import com.flyduck.common.util.TreeUtil;
import com.flyduck.system.dao.SysDeptDao;
import com.flyduck.system.exception.SystemExceptionCode;
import com.flyduck.system.vo.dept.*;
import com.flyduck.system.vo.dept.trans.*;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.flyduck.system.entity.SysDeptEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysDeptService{

    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private TokenUtil tokenUtil;

    //************************* 获取部门树开始 *************************
    /**
     * 获取部门树
     * @param reqVo
     * @return
     */
    public List<SysDeptTreeRespVO> getDeptTreeList(SysDeptTreeReqVO reqVo) {
        List<SysDeptEntity> deptList = getDeptList(reqVo);
        List<SysDeptTreeRespVO> sysDeptTreeRespVOS = Lists.transform(deptList, new SysDeptEntityToSysDeptTreeRespVO());
        List<SysDeptTreeRespVO> deptTree = null;
        deptTree = TreeUtil.toTree(SysDeptTreeRespVO.class,sysDeptTreeRespVOS);
        return deptTree;
    }

    private List<SysDeptEntity> getDeptList(SysDeptTreeReqVO reqVo) {
        QueryWrapper<SysDeptEntity> wrapper = getDeptQueryWrapper(reqVo);
        return sysDeptDao.list(wrapper);
    }

    private QueryWrapper<SysDeptEntity> getDeptQueryWrapper(SysDeptTreeReqVO reqVo) {
        QueryWrapper<SysDeptEntity> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotBlank(reqVo.getDeptName())) {
            wrapper.like("dept_name", reqVo.getDeptName());
        }
        return wrapper;
    }

    //************************* 获取部门树结束 *************************

    /**
     * 获取部门下拉树
     * @return
     */
    public List<SysDeptTreeSelectRespVO> getDeptTreeSelect() {
        List<SysDeptEntity> deptList = sysDeptDao.findAllDept();
        List<SysDeptTreeSelectRespVO> deptTreeSelectRespVOList = Lists.transform(deptList, new SysDeptEntityToSysDeptTreeSelectRespVO());
        List<SysDeptTreeSelectRespVO> deptTree = null;
        try {
            deptTree = TreeUtil.toTree(SysDeptTreeSelectRespVO.class,deptTreeSelectRespVOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deptTree;
    }

    /**
     * 新增部门
     * @param reqVo
     * @return
     */
    public boolean addDept(SysDeptAddReqVO reqVo) {
        SysDeptEntity deptEntity = new SysDeptAddReqVOToSysDeptEntity().apply(reqVo);
        if(ObjectUtil.isNull(reqVo.getParentId())){
            deptEntity.setAncestors(reqVo.getId());
        }else {
            SysDeptEntity parentDept = sysDeptDao.findDeptById(deptEntity.getParentId());
            deptEntity.setAncestors(parentDept.getAncestors()+","+reqVo.getId());
        }
        deptEntity.setCreateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        deptEntity.setCreateTime(new Date());
        deptEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        deptEntity.setUpdateTime(new Date());
        return sysDeptDao.save(deptEntity);
    }

    /**
     * 获取部门详情
     * @param id
     * @return
     */
    public SysDeptDetailRespVO getDeptDetail(Long id) {
        SysDeptEntity deptEntity = sysDeptDao.findDeptById(id);
        return new SysDeptEntityToSysDeptDetailRespVO().apply(deptEntity);
    }

    /**
     * 修改部门(不能修改部门编码)
     * @param reqVo
     * @return
     */
    @Transactional
    public boolean updateDept(SysDeptUpdateReqVO reqVo) {
        SysDeptEntity deptEntity = new SysDeptUpdateReqVOToSysDeptEntity().apply(reqVo);
        if(ObjectUtil.isNull(reqVo.getParentId())){
            deptEntity.setAncestors(reqVo.getId());
        }else {
            SysDeptEntity parentDept = sysDeptDao.findDeptById(deptEntity.getParentId());
            deptEntity.setAncestors(parentDept.getAncestors()+","+reqVo.getId());
        }
        deptEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
        deptEntity.setUpdateTime(new Date());
        //修改子部门的祖籍部门
        updateAncestor(deptEntity);
        return sysDeptDao.updateById(deptEntity);
    }

    private void updateAncestor(SysDeptEntity deptEntity) {
        //获取子部门
        List<SysDeptEntity> childrenDept = sysDeptDao.findByParentId(deptEntity.getId());
        if(CollectionUtil.isEmpty(childrenDept)){
            return;
        }
        for (SysDeptEntity sysDeptEntity : childrenDept) {
            sysDeptEntity.setAncestors(deptEntity.getAncestors()+","+sysDeptEntity.getId());
            deptEntity.setUpdateName(tokenUtil.getLoginInfo().getUserInfo().getNickName());
            deptEntity.setUpdateTime(new Date());
            sysDeptDao.updateById(sysDeptEntity);
            updateAncestor(sysDeptEntity);
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    public boolean deleteDept(Long id) {
        List<SysDeptEntity> childrenDept = sysDeptDao.findByParentId(id);
        if (CollectionUtil.isNotEmpty(childrenDept)) {
            throw new BusinessException(SystemExceptionCode.EXIST_CHILDREN_DEPTS);
        }
        return sysDeptDao.removeById(id);
    }




}
